<?php

require_once("../../../lib/class.server.php");
require_once("../../../lib/dbutil.inc.php");
require_once("../../../lib/class.inputfilter.php");

new Controller();

class Controller {

    private $cnx = NULL;
    private $filter = NULL;

    public function __construct() {
        $this->cnx = new cDB();
        $this->filter = new InputFilter();
        $this->conectar();
        $this->filter();
        $this->procesar();
    }

    private function filter() {
        $_POST = $this->filter->process($_POST);
        $_POST = $this->filter->safeSQL($_POST, $this->cnx->link);
        $_GET = $this->filter->process($_GET);
        $_GET = $this->filter->safeSQL($_GET, $this->cnx->link);
        $_REQUEST = $this->filter->process($_REQUEST);
        $_REQUEST = $this->filter->safeSQL($_REQUEST, $this->cnx->link);
    }

    public function __destruct() {
        $this->cnx->Disconnect();
        $this->filter = NULL;
    }

    function conectar() {
        global $server;
        $this->cnx->Connect($server['server'], $server['db'], $server['user'], $server['pass']);
    }

    private function procesar() {

        if (filter_has_var(INPUT_POST, 'update')) {
            echo $this->update();
        }

        if (filter_has_var(INPUT_POST, 'getAgenda')) {
            echo $this->getAgenda();
        }

        if (filter_has_var(INPUT_POST, 'agregar')) {
            echo $this->agregar();
        }

        if (filter_has_var(INPUT_POST, 'ids')) {
            echo $this->baja();
        }

        if (filter_has_var(INPUT_POST, 'info')) {
            echo $this->getInfo();
        }
		if (filter_has_var(INPUT_POST, 'getRubro')) {
            echo $this->getRubro();
        }
		
        if (filter_input_array(INPUT_POST) && count(filter_input_array(INPUT_POST)) > 0) {
            exit();
        }
    }

    private function update() {
        $sql = <<<SQL
UPDATE comercios SET id_rubro='{$_POST['id_rubro']}',nombre='{$_POST['nombre']}',direccion='{$_POST['direccion']}',detalle='{$_POST['detalle']}',telefono='{$_POST['telefono']}',celular='{$_POST['celular']}', email='{$_POST['email']}' WHERE id = '{$_POST['id']}'
SQL;
        if ($this->cnx->Query($sql)) {
            return json_encode(array("success" => 1, "message" => 'Contacto actualizado de manera correcta'));
        } else {
            return json_encode(array("success" => 0, "message" =>'Error, algo salio mal intentelo de nuevo.', "sql" => $sql, "messageError" => mysql_errno() . ': ' . mysql_error()));
        }
    }

    private function agregar() {
        $sql = <<<SQL
INSERT INTO comercios VALUES (NULL,'%s', '%s', '%s', '%s', sysdate(), '%s', '%s', '%s')
SQL;
        $sql = sprintf($sql, filter_input(INPUT_POST, 'id_rubro'), filter_input(INPUT_POST, 'nombre'), filter_input(INPUT_POST, 'direccion'), filter_input(INPUT_POST, 'detalle'), filter_input(INPUT_POST, 'telefono'), filter_input(INPUT_POST, 'celular'), filter_input(INPUT_POST, 'email'));
        if ($this->cnx->Query($sql)) {
            return json_encode(array("success" => 1, "message" => 'Contacto agregado de manera correcta'));
        } else {
            return json_encode(array("success" => 0, "message" => 'Error, algo salio mal intentelo de nuevo.', "sql" => $sql, "messageError" => mysql_errno() . ': ' . mysql_error()));
        }
    }

    private function baja() {
        $data = array();
        $sql = <<<SQL
DELETE FROM comercios WHERE id = '%s'             
SQL;
        $sql = sprintf($sql, filter_input(INPUT_POST, 'ids'));
        if ($this->cnx->Query($sql)) {
            $data = array("success" => 1, "message" => 'Contacto eliminado correctamente.');
        } else {
            $data = array("success" => 0, "message" => 'Error, algo salio mal intentelo de nueva cuenta', 'sql' => $sql);
        }
        return json_encode($data);
    }

    private function getAgenda() {
        $data = array();
        $sql = <<<SQL
SELECT * FROM comercios a ORDER BY a.id ASC               
SQL;
        if ($this->cnx->Query($sql)) {
            while ($fila = $this->cnx->Next()) {
                $data[] = $fila;
            }
        } else {
            $data = array("success" => 0, "message" => 'Error, algo salio mal intentelo de nueva cuenta', 'sql' => $sql);
        }
        return json_encode($data);
    }

    private function getRubro() {
        $data = array();
        $sql = <<<SQL
SELECT a.rubro, b.id_rubro, b.nombre, b.direccion, b.detalle, b.fecha_alta, b.telefono, b.celular, b.email from rubros a, comercios b where a.id = b.id_rubro;
SQL;
        if ($this->cnx->Query($sql)) {
            while ($fila = $this->cnx->Next()) {
                $data[] = $fila;
            }
        } else {
            $data = array("success" => 0, "message" => 'Error, algo salio mal intentelo de nueva cuenta', 'sql' => $sql);
        }
        return json_encode($data);
    }
	
	
    private function getInfo() {
        $data = array();
        $sql = <<<SQL
SELECT * FROM comercios a WHERE a.id = {$_POST['info']} ORDER BY a.id ASC               
SQL;
        if ($this->cnx->Query($sql)) {
            if ($fila = $this->cnx->First()) {
                $data = $fila;
            }
        } else {
            $data = array("success" => 0, "message" => 'Error, algo salio mal intentelo de nueva cuenta', 'sql' => $sql);
        }
        return json_encode($data);
    }

}
